/*
* mainwin.h - This file is the part of the wp-notifier.(code name "luan luan")
* Copyright (C) 2010 Dean Lee <deanlee3@gmail.com>, http://www.deanlee.cn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
* Acknowledgements:
*   <> Many thanks to all of you, who have encouraged me to update my articles
*     and code, and who sent in bug reports and fixes.
* 
*/
#ifndef MAINWIN_H
#define MAINWIN_H
#include <QWidget>
#include <QSystemTrayIcon>
#include "ui_notifier.h"

class QLabel;
class QStatusBar;
class QProgressBar;
class CommentsModel;
class CommentFilterModel;

class BlogModelList : public QObject
{
	Q_OBJECT
public:
	BlogModelList(QObject* parent = NULL);
	~BlogModelList();
	QList<CommentsModel*> models();
	void addModel(CommentsModel* m);
	void updateModel(CommentsModel* m);
	void deleteModel(CommentsModel* m);
signals:
	void modelUpdated(CommentsModel* m);
	void modelAdded(CommentsModel* m);
	void modelDeleted(CommentsModel* m);
protected:
	QList<CommentsModel*> m_models;
};

class MainWindow : public QWidget
{
	Q_OBJECT
public:
	MainWindow(QWidget* parent = NULL);
	~MainWindow();
	static MainWindow* instance(){return s_instance;}
	QAction* action(const QString& name);
	QList<CommentsModel*> models()const{return m_models;}
public slots:
	void busy(const QString& message);
	void accountChanged(Blog* blog);
	void accountDeleted(Blog* blog);
	void saveOptions();
	void noBusy();
protected slots:
	void refresh();
	void popupClicked();
	void checkForUpdate();
	void updateAvailable(const QString&);
	void raiseMainwnd();
	void flashTrayIcon();
	void applyStyleSheet();
	void trayActivated(QSystemTrayIcon::ActivationReason);
	void actAbout();
	void hasNewComments(int, int ,int);
	void closeProgram();
	void setChecked(bool);
	void doPostComment();
	void replyComment(const QModelIndex&);
	void doConfig();
	void currentIndexChanged(int);
	void commentsCountChanged();
protected:
	void contextMenuEvent(QContextMenuEvent *);
	void showEvent(QShowEvent * event);
	void buildActions();
	bool loadOptions();
	void resizeEvent(QResizeEvent *e);
	bool eventFilter(QObject*, QEvent*);
	void positionToRightBottom();
	void createTray();
	void closeEvent(QCloseEvent *e);

	Ui::Notifier ui_;
	//CommentsModel* m_model;
	QList<CommentsModel*> m_models;
	CommentFilterModel* m_filterModel;
	CommentItemDelegate* m_delegate;
	QSystemTrayIcon* m_tray;
	QTimer* m_refreshTimer;
	QTimer* m_trayIconFlashTimer;
	QPersistentModelIndex m_replyIndex;
	QMap<QString, QAction*> m_actionMap;
	static MainWindow* s_instance;
	QIcon* m_trayIcon1;
	QIcon* m_trayIcon2;
	QMovie* m_loadingMovie;

	//Blog* m_currentBlog;
};
//#if defined(Q_WS_WIN)

#include <QThread>
#include <windows.h>
class StyleWatchThread: public QThread
{
	Q_OBJECT
public:
	StyleWatchThread(const QString& dir, QObject* parent) : QThread(parent)
	{
		m_dir = dir;
	}
	void run()
	{
		quint32 dwWaitStatus; 
		HANDLE hNotifier; 
		char lpDrive[4];
		char lpFile[_MAX_FNAME];
		char lpExt[_MAX_EXT];
		//char* lpDir = (char*)param;

		_splitpath_s(qPrintable(m_dir), lpDrive, 4, NULL, 0, lpFile, _MAX_FNAME, lpExt, _MAX_EXT);

		lpDrive[2] = (wchar_t)'\\';
		lpDrive[3] = (wchar_t)'\0';

		// Watch the directory for file creation and deletion. 

		hNotifier = FindFirstChangeNotificationA( 
			qPrintable(m_dir),                         // directory to watch 
			TRUE,                         // watch subtree 
			FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE /*| FILE_NOTIFY_CHANGE_SIZE*/
			); // watch file name changes 

		if (hNotifier == INVALID_HANDLE_VALUE) 
			ExitProcess(GetLastError()); 

		while (TRUE) 
		{ 
			// Wait for notification.
			dwWaitStatus = WaitForSingleObject(hNotifier, INFINITE); 

			switch (dwWaitStatus) 
			{ 
			case WAIT_OBJECT_0: 

				// A file was created or deleted in the directory.
				// Refresh this directory and restart the notification.
				emit styleChanged();

				//RefreshDirectory(lpDir); 
				if ( FindNextChangeNotification( 
					hNotifier) == FALSE ) 
					ExitProcess(GetLastError()); 
				break; 

			default: 
				ExitProcess(GetLastError()); 
			}
		}
	}
signals:
	void styleChanged();
protected:
	QString m_dir;
};
//#endif


#endif